//
//  PicViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "PicViewController.h"
#import "PicTableViewCell.h"
#import "PicModel.h"
#import "PicDetailViewController.h"
@interface PicViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *picArray;
@property (nonatomic,copy)   NSString *urlString;
@property (nonatomic,copy)   NSString *webUrl;
@property (nonatomic,assign) BOOL isClick;//判断点赞状态
@end

@implementation PicViewController

static NSString *cellIdnetifier = @"picClassCell";

- (NSMutableArray *)picArray
{
    if (!_picArray) {
        _picArray = [[NSMutableArray alloc]init];
    }
    return _picArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self nextUrl:kFunPic];
    [self.tableView reloadData];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //self.tableView.allowsSelection = NO;
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    
//    GroundGlass *backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
//    self.tableView.backgroundView = backImage;

    //上拉加载
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    self.tableView.footer = footer;
    //下来刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    self.tableView.header = header;
    [self.tableView registerClass:[PicTableViewCell class] forCellReuseIdentifier:cellIdnetifier];
    
    self.isClick = YES;
}
//下拉刷新
- (void)headerAction
{
    [self nextUrl:kFunPic];
    [self.tableView.header endRefreshing];
}
//上拉加载
- (void)footerAction
{
    NSLog(@"next_url == %@",_urlString);
    [self nextUrl:_urlString];
    
    [self.tableView.footer endRefreshing];
}

- (void)nextUrl:(NSString *)url
{
    if ([url isEqualToString:kFunPic]) {
        [self.picArray removeAllObjects];
    }
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = [response objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"articles"];
        NSDictionary *dicss = [dic objectForKey:@"info"];
        _urlString = [dicss objectForKey:@"next_url"];
        NSMutableArray *picArr = [NSMutableArray array];
        for (NSMutableDictionary *dict in array) {
            NSArray *arr = [dict objectForKey:@"media"];
            self.webUrl = [dict objectForKey:@"weburl"];
            for (NSMutableDictionary *dics in arr) {
                PicModel *model = [[PicModel alloc]init];
                model.title = [dict objectForKey:@"title"];
                model.thumbnail_pic = [dics objectForKey:@"url"];
                model.detail_url = [dics objectForKey:@"raw_url"];
                [picArr addObject:model];
            }
        }
        [self.picArray addObjectsFromArray:picArr];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.picArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdnetifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    PicModel *model = _picArray[indexPath.row];
    
    [cell.shareButton addTarget:self action:@selector(shareButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [cell.goodButton addTarget:self action:@selector(goodButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell cellSetModel:model];

    
    return cell;
}
- (void)goodButtonAction:(UIButton *)but
{
    if (_isClick == YES) {
        [MBProgressHUD showError:@"+1"];
        [but setBackgroundImage:[UIImage imageNamed:@"Thumb-orange-16@2x.png"] forState:UIControlStateNormal];
    }
    else
    {
        [MBProgressHUD showError:@"-1"];
        [but setBackgroundImage:[UIImage imageNamed:@"Thumb-gray-16@2x.png"] forState:UIControlStateNormal];
    }
    _isClick = !_isClick;
    
}

- (void)shareButtonAction
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55a87dbd67e58eb9b30039ad"
                                      shareText:self.webUrl
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,nil]delegate:self];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicDetailViewController *picDetailVC = [[PicDetailViewController alloc]init];
    PicModel *model = [self.picArray objectAtIndex:indexPath.row];
    picDetailVC.raw_url = model.detail_url;
    picDetailVC.weburl = self.webUrl;
    picDetailVC.titleText = model.title;
    [self.navigationController pushViewController:picDetailVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicModel *model = self.picArray[indexPath.row];
   PicTableViewCell *cell = [[PicTableViewCell alloc]init];
    
   return  [cell height:model];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
